package cn.icyzx.dao;

import cn.icyzx.models.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;

/**
 * Created by zk on 2016/10/5.
 */
@Repository("daoFindPsw")
public class DaoFindPsw {
    /**
     * send email to reset-psw
     */
    @Autowired
    SendMail sendMail;

    @Autowired
    DaoUser daoUser;

    @Autowired
    UserMapper userMapper;

    public boolean sendMailToFindPsw(String email) {
        try {
            User user = daoUser.getUserByEmail(email);
            if (user == null) return false;
            /**
             *   配置找回密码密钥
             * */
            user.setFindPsw(String.valueOf(new Date().getTime()));
            userMapper.savefindPsw(user);
            /**
             * 发送找回密码邮件
             */
            if (sendMail.sendEmailToFindPws(user)) return true;
            else return false;

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }


    public boolean checkfindPsw(String uid, String findPsw) {
        User user = new User();
        user.setUid(uid);
        user.setFindPsw(findPsw);
        User user1 = userMapper.checkfindPsw(user);

        return user1 == null ? false : true;
    }


    public boolean modifyPsw(String password, String uid) {
        try {
            User user = new User();

            user.setUid(uid);
            user.setPassword(password);
            userMapper.modifyPsw(user);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
